package com.yanqu.road.dao.impl.match;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.match.MatchData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：zfr
 * @date ：2021/2/18 17:18
 */
public class MatchDataDaoImpl extends TempDataObjectDao<MatchData> {
    public static String addSql = "INSERT INTO t_c_variety_data(groupId, timeId, level, type, id, matchId, serverList, isOverForGroupChange)" +
            "VALUES(?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(MatchData data) {
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTimeId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMatchId()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getServerList(), ",")));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isOverForGroupChange()));
        if (getDb().executeNoneQuery(addSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE t_c_variety_data SET isOverForGroupChange = ? WHERE groupId=? AND timeId = ? AND id = ?";
    @Override
    protected boolean updateData(MatchData data) {
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isOverForGroupChange()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTimeId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected MatchData parseData(ResultSet rs) throws SQLException {
        MatchData data = new MatchData();
        data.setGroupId(rs.getLong("groupId"));
        data.setTimeId(rs.getInt("timeId"));
        data.setLevel(rs.getInt("level"));
        data.setType(rs.getInt("type"));
        data.setId(rs.getInt("id"));
        data.setMatchId(rs.getInt("matchId"));
        String value = rs.getString("serverList");
        data.setServerList(StringUtils.stringToLongList(value, ","));
        data.setOverForGroupChange(rs.getBoolean("isOverForGroupChange"));
        return data;
    }

    public static String selectSql = "SELECT * FROM t_c_variety_data WHERE timeId > ?";
    public List<MatchData> getMatchDataList(int timeId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<MatchData> matchDataList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setInt(1, timeId);
            rs = call.executeQuery();
            while (rs.next()) {
                matchDataList.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return matchDataList;
    }

    public static String getMaxIdSql = "select ifnull(max(id),0) as maxId from t_c_variety_data";
    public int getMatchMaxId() {
        ResultSet rs = null;
        CallableStatement call = null;
        int maxId = 0;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getMaxIdSql);
            rs = call.executeQuery();
            while (rs.next()) {
                maxId = rs.getInt("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return maxId;
    }
}
